﻿using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Orm.DataEntity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Kingdee.BOS.Util;
using System.ComponentModel;

namespace LiMinCRMPlugin
{
    /// <summary>
    /// 类名或功能模块简述
    /// </summary>
    /// <remarks>
    /// 功能：当生产汇报单反审核时，查询子单据体的数量更新到原始数据录入表中的生产汇报数量字段中
    /// 创建人：张虎
    /// 创建日期：2024/6/5 15:28:32
    /// 修改信息（1）：张虎，2024/6/5 15:28:32，修改内容
    /// </remarks>
    /// 
    [Description("当生产汇报单反审核时，查询子单据体的数量更新到原始数据录入表中的生产汇报数量字段中")]
    [HotUpdate]
    public  class MORPTUnAuditService : AbstractOperationServicePlugIn
    {
        public override void OnPreparePropertys(PreparePropertysEventArgs e)
        {
            base.OnPreparePropertys(e);
            e.FieldKeys.Add("F_QLTK_SrcEntryId");//子单据体源单分录ID
            e.FieldKeys.Add("F_QLTK_NUMBER");//子单据体数量
        }
        public override void AfterExecuteOperationTransaction(AfterExecuteOperationTransaction e)
        {
            foreach (DynamicObject entity in e.DataEntitys)
            {
                DynamicObjectCollection FEntity = entity["PRD_MORPTENTRY"] as DynamicObjectCollection;//生产汇报单明细
                foreach (var entryRow in FEntity)
                {
                    DynamicObjectCollection dycDetailEntry = entryRow["FRawDateEntity"] as DynamicObjectCollection;//原始数据录入表信息
                    foreach (var entry in dycDetailEntry)
                    {
                        double F_QLTK_Number = double.Parse(Convert.ToString(entry["F_QLTK_Number"]));//数量
                        string F_QLTK_SrcEntryId = Convert.ToString(entry["F_QLTK_SrcEntryId"]);//原始数据录入表分录ID
                        string sql = string.Format(@"select * from demo_t_Cust_Entry100524 where FDetailID='" + F_QLTK_SrcEntryId + "'");
                        DataTable sql_toStirng = DBUtils.ExecuteDataSet(this.Context, sql).Tables[0];
                        if (sql_toStirng.Rows.Count > 0)
                        {
                            double F_QLTK_REPORTCOUNT = double.Parse(sql_toStirng.Rows[0]["F_QLTK_REPORTCOUNT"].ToString());//生产汇报数量
                            double number = F_QLTK_REPORTCOUNT - F_QLTK_Number;//生产汇报数量-数量
                            DBUtils.Execute(Context, "update demo_t_Cust_Entry100524 set F_QLTK_REPORTCOUNT='" + number + "' where FDetailID='" + F_QLTK_SrcEntryId + "'");
                        }
                    }
                }
            }
        }
    }
}
